package demo;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class CardGame {
    public static final String[] suits = {"♠","♣","♦","♥"};
    //买牌
    public List<Card> buyCard() {
        //生成一个一维数组
        List<Card> cardList = new ArrayList<>();

        for(int i = 0;i < 4;i++) {//花色
            for(int j  = 1;j <= 13;j++) {//张数
                    String suit = suits[i];
                    Card card = new Card(suit,j);
                    cardList.add(card);
                    //cardList.add(new Card(suits[i],j));
            }
        }
        return cardList;
    }
    //洗牌
    public void shuffle(List<Card> cardList) {
        //生成随机数
        Random random = new Random();

        for(int i = cardList.size() - 1;i > 0;i--) {
            int index = random.nextInt(i);
            //交换
            swap(cardList,i,index);
        }
    }

    private static void swap(List<Card> cardList,int i,int j) {
        Card tmp = cardList.get(i);
        cardList.set(i,cardList.get(j));
        cardList.set(j,tmp);
    }

    //揭牌
    public List<List<Card>> getCard(List<Card> cardList) {
        //新建一维数组存储三人的牌
        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();
        List<Card> hand3 = new ArrayList<>();

        List<List<Card>> hand = new ArrayList<>();
        hand.add(hand1);
        hand.add(hand2);
        hand.add(hand3);

        //获得牌 3人各五张
        for(int i = 0;i < 5;i++) {
            for(int j = 0; j < 3;j++) {
                Card card = cardList.remove(0);//获取每次0下标 并完成覆盖
                ////放进二维数组中，根据下标就可以知道设获取五张牌
                hand.get(j).add(card);
            }
        }
        return hand;
    }
}
